﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MVC4App.Repositories.Core;
using MVC4App.Repositories.Core.Security;
using MVC4App.SecurityServiceReference;

namespace MVC4App.Repositories.Implementations.Security
{
    public class PermissionRepository:RepositoryBase, IPermissionRepository
    {
        public List<PermissionDto> GetList(Criterion criterion = null)
        {
            var request = new PermissionRequest().Prepare();
            request.LoadOptions = new[] { "Permissions" };
            var response = SecurityServiceClient.GetPermissions(request);
            Correlate(request, response);
            return response.Permissions == null ? null : response.Permissions.ToList();
        }

        public List<PermissionDto> GetQuickSearchPaged(ref int totalPage, int currentPage = 1, string keyword = null)
        {
            throw new NotImplementedException();
        }

        public List<PermissionDto> GetPaged(ref int totalPage, int currentPage = 1)
        {
            throw new NotImplementedException();
        }

        public int GetPageSize()
        {
            var request = new PermissionRequest().Prepare();
            request.LoadOptions = new[] { "TotalPage" };
            var response = SecurityServiceClient.GetPermissions(request);
            Correlate(request, response);
            return response.TotalPage;
        }

        public List<PermissionDto> GetPaged(int currentPage=1)
        {
            var request = new PermissionRequest().Prepare();
            request.LoadOptions = new[] { "Permissions" };
            request.OptionTypes = OptionType.IsPaging;
            request.CurrentPage = currentPage;
            var response = SecurityServiceClient.GetPermissions(request);
            Correlate(request, response);
            return response.Permissions == null ? null : response.Permissions.ToList();
        }

        public List<PermissionDto> GetByUnit(int unitId)
        {
            throw new NotImplementedException();
        }

        public PermissionDto Get(int id)
        {
            var request = new PermissionRequest().Prepare();
            request.LoadOptions = new[] { "Permission" };
            request.Criteria=new PermissionCriteria(){Id = id};
            var response = SecurityServiceClient.GetPermissions(request);
            Correlate(request, response);
            return response.Permission ?? null;
        }

        public int GetCount(Criterion criterion = null)
        {
            throw new NotImplementedException();
        }

        public string Insert(PermissionDto t)
        {
            var request = new PermissionRequest().Prepare();
            request.Action = "Create";
            request.Permission = t;
            var response = SecurityServiceClient.SetPermissions(request);
            Correlate(request, response);
            return response.Message;
        }

        public string Update(PermissionDto t)
        {
            var request = new PermissionRequest().Prepare();
            request.Action = "Update";
            request.Permission = t;
            var response = SecurityServiceClient.SetPermissions(request);
            Correlate(request, response);
            return response.Message;
        }

        public string Delete(int id)
        {
            var request = new PermissionRequest().Prepare();
            request.Action = "Delete";
            request.Criteria=new PermissionCriteria(){Id = id};
            var response = SecurityServiceClient.SetPermissions(request);
            Correlate(request, response);
            return response.Message;
        }

        public PermissionDto CheckExistsInRoleInSite(int siteId, string perCode, int userId)
        {
            var request = new PermissionRequest().Prepare();
            request.LoadOptions = new[] { "CheckExistsInRoleInSite" };
            request.Criteria = new PermissionCriteria() { SiteId = siteId, Code = perCode, UserId = userId };
            var response = SecurityServiceClient.GetPermissions(request);
            Correlate(request, response);
            return response.Permission;            
        }

        public IList<PermissionDto> GetListByUserAndSite(int userId, int siteId)
        {
            var request = new PermissionRequest().Prepare();
            request.LoadOptions = new[] { "GetByUserAndSite" };
            request.Criteria = new PermissionCriteria() { UserId = userId,SiteId = siteId};
            var response = SecurityServiceClient.GetPermissions(request);
            Correlate(request, response);
            return response.Permissions;
        }

        public string GrantPermission(List<PermissionInSiteDto> objs)
        {
            throw new NotImplementedException();
        }
    }
}